/**
 * WhitePlayer represents the white player in a game of chess.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */

package lp.chess;

public class WhitePlayer extends Player {
	//- CONSTRUCTOR(S) ------------------------------------------------------------------------

	/**
 	 * Default constructor.
 	 * Initializes a white player with all of its pieces.
 	 */
	public WhitePlayer()
	{
		m_pieces = new Piece[16];
		
		// First row :
		m_pieces[0] = new Rook(1, 8, true);
		m_pieces[1] = new Knight(2, 8, true);
		m_pieces[2] = new Bishop(3, 8, true);
		m_pieces[3] = new Queen(4, 8, true);
		m_pieces[4] = new King(5, 8, true);
		m_pieces[5] = new Bishop(6, 8, true);
		m_pieces[6] = new Knight(7, 8, true);
		m_pieces[7] = new Rook(8, 8, true);
		
		// Second row :
		m_pieces[8] = new Pawn(1, 7, true);
		m_pieces[9] = new Pawn(2, 7, true);
		m_pieces[10] = new Pawn(3, 7, true);
		m_pieces[11] = new Pawn(4, 7, true);
		m_pieces[12] = new Pawn(5, 7, true);
		m_pieces[13] = new Pawn(6, 7, true);
		m_pieces[14] = new Pawn(7, 7, true);
		m_pieces[15] = new Pawn(8, 7, true);
	}
	
	/**
 	 * Copy constructor.
 	 * Initializes a white player with all of its pieces, given another one.
 	 * 
 	 * @param other the other player to copy in this one.
 	 */
	public WhitePlayer(WhitePlayer other) {
		m_pieces = new Piece[16];
		
		// First row :
		m_pieces[0] = new Rook((Rook)other.getPieces()[0]);
		m_pieces[1] = new Knight((Knight)other.getPieces()[1]);
		m_pieces[2] = new Bishop((Bishop)other.getPieces()[2]);
		m_pieces[3] = new Queen((Queen)other.getPieces()[3]);
		m_pieces[4] = new King((King)other.getPieces()[4]);
		m_pieces[5] = new Bishop((Bishop)other.getPieces()[5]);
		m_pieces[6] = new Knight((Knight)other.getPieces()[6]);
		m_pieces[7] = new Rook((Rook)other.getPieces()[7]);
		
		// Second row :
		m_pieces[8] = new Pawn((Pawn)other.getPieces()[8]);
		m_pieces[9] = new Pawn((Pawn)other.getPieces()[9]);
		m_pieces[10] = new Pawn((Pawn)other.getPieces()[10]);
		m_pieces[11] = new Pawn((Pawn)other.getPieces()[11]);
		m_pieces[12] = new Pawn((Pawn)other.getPieces()[12]);
		m_pieces[13] = new Pawn((Pawn)other.getPieces()[13]);
		m_pieces[14] = new Pawn((Pawn)other.getPieces()[14]);
		m_pieces[15] = new Pawn((Pawn)other.getPieces()[15]);
	}
	
	//-----------------------------------------------------------------------------------------

	/**
	 * Gets the pieces owned by this white player.
	 * 
	 * @return the array of all the pieces owned by this white player.
	 */
	public Piece[] getPieces() {
		return m_pieces;
	}
	
	/**
	 * Returns true if this white player is in check.
	 * 
	 * @param cb the chessboard.
	 * @return true if this white player is in check.
	 */
	public boolean isInCheck(Chessboard cb) {
		int i = 0;
		int xk, yk;
		int blackPiecesTested = 0;
		boolean check = false;
		
		// First we retrieve the white player's king and it's coordinates:
		Piece wk = m_pieces[4];
		xk = wk.getX();
		yk = wk.getY();
		
		// Then we search the chessboard for a piece that could take over the king during the next turn:
		while(i < 64 && blackPiecesTested < 16 && !check) {	// The searching will stop if we have reached the last case or if every black piece has been checked.
			if(cb.getCases()[i] != null && cb.getCases()[i].isBlack()) {	// There is a piece on this case and it's a black one:
				if(cb.getCases()[i].validMove(cb, xk, yk, false)) {
					check = true;
				}
				blackPiecesTested++;
			}
			
			i++;
		}
		
		/**
		 * Returns true if this white player is checkmate.
		 * 
		 * @param cb the chessboard.
		 * @return true if this white player is checkmate.
		 */
		if(check) wk.display();
		
		return check;
	}
	
	public boolean isCheckmate(Chessboard cb) {
		return true;
	}
	
	// DISPLAY METHOD(S) ----------------------------------------------------------------------

	/**
	 * Displays the white player with all of its pieces.
	 */
	public void display()
	{
		for(int i=0 ; i<16 ; i++)
		{
			System.out.println("Piece " + i+1 + " : ");
			m_pieces[i].display();
		}
	}
	
	//-----------------------------------------------------------------------------------------
}